package de.gwtprime.client.widgets.trading;

import java.util.List;
import java.util.Set;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.ListDataProvider;

import de.gwtprime.shared.requestFactory.TransactionProxy;

public class TransactionOverview extends Composite {

	private static TransactionOverviewUiBinder uiBinder = GWT
			.create(TransactionOverviewUiBinder.class);

	interface TransactionOverviewUiBinder extends
	UiBinder<Widget, TransactionOverview> {}

	@UiField
	CellTable<TransactionProxy> table;

	final  ListDataProvider<TransactionProxy> transactionProvider = new ListDataProvider<TransactionProxy>();
	List<TransactionProxy> transactions = transactionProvider.getList();

	public TransactionOverview(Set<TransactionProxy> set) {
		initWidget(uiBinder.createAndBindUi(this));
		
		TextColumn<TransactionProxy> typeColumn = new TextColumn<TransactionProxy>() {
			@Override
			public String getValue(TransactionProxy object) {
				return String.valueOf(object.getType().toString());
			}
		};
		
		table.addColumn(typeColumn, "type");
		
		TextColumn<TransactionProxy> amountColumn = new TextColumn<TransactionProxy>() {
			@Override
			public String getValue(TransactionProxy object) {
				return String.valueOf(object.getAmount());
			}
		};
		
		table.addColumn(amountColumn, "amount");
		
		
		TextColumn<TransactionProxy> timeColumn = new TextColumn<TransactionProxy>() {
			@Override
			public String getValue(TransactionProxy object) {
				return String.valueOf(object.getTime().toString());
			}
		};
		
		table.addColumn(timeColumn, "time");

		
		TextColumn<TransactionProxy> confirmedColumn = new TextColumn<TransactionProxy>() {
			@Override
			public String getValue(TransactionProxy object) {
				return String.valueOf(object.isConfirmed() ? "Bestätigt": "Pending");
			}
		};
		
		table.addColumn(confirmedColumn, "confirmed");
		
		transactionProvider.addDataDisplay(table);

		updateTransactionInTable(set);
	}

	/**
	 * 
	 * @param newTransactions
	 */
	public void updateTransactionInTable(Set<TransactionProxy> newTransactions) {

		transactions.clear();

		for (TransactionProxy transactoin : newTransactions) {
			transactions.add(transactoin);
		}
	}
}
